package com.danceforyou.${objectName?uncap_first}.controller;

import com.danceforyou.common.util.AssertUtil;
import com.danceforyou.common.util.Constants;
import com.danceforyou.common.util.StringUtil;
import com.danceforyou.common.dto.SqlOrderEnum;
import com.danceforyou.common.model.view.BizData4Page;
import com.danceforyou.common.restful.exception.BizExceptionEnum;
import com.danceforyou.${objectName?uncap_first}.domain.${objectName};
import com.danceforyou.${objectName?uncap_first}.service.${objectName}Service;
import com.danceforyou.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * 类名称：${objectName}Controller
 * 创建人：danceforyou
 * 创建时间：${nowDate?string("yyyy年MM月dd日")}
 * 版本号：1.0v
 */
@RestController
@RequestMapping("/${objectName?uncap_first}")
public class ${objectName}Controller {

    @Autowired
    private ${objectName}Service ${objectName?uncap_first}Service;
	@Autowired
	private HttpSession session;

    /**
	 * 分页查询
	 * @param conditions 查询条件
	 * @return 查询结果
	 */
    @RequestMapping("/queryPage")
    public BizData4Page queryPage(@RequestParam Map<String, Object> conditions) {
        User loginOper = (User) session.getAttribute(Constants.YK_USER);
		AssertUtil.notNull(loginOper, BizExceptionEnum.AUTHFAILURE);
        for (Map.Entry<String, Object> entry : conditions.entrySet()) {
		    if (StringUtil.isblank(entry.getValue())) {
                conditions.put(entry.getKey(), null);
            }
        }
        if (loginOper.getCompany_id() > 0) {
        	conditions.put("company_id", loginOper.getCompany_id());
		}
        BizData4Page<Map<String, Object>> bizData4Page = new BizData4Page<>();
        bizData4Page.setPage(Integer.parseInt(String.valueOf(conditions.get("pageNo"))));
        bizData4Page.setPagesize(Integer.parseInt(String.valueOf(conditions.get("pageSize"))));
        bizData4Page.setConditions(conditions);
        if (StringUtil.isblank(conditions.get("order")) || conditions.get("order").toString().toUpperCase().equals("ASC")) {
            ${objectName?uncap_first}Service.queryPageByDataPerm(bizData4Page, conditions.get("sort").toString(), SqlOrderEnum.ASC);
        } else {
            ${objectName?uncap_first}Service.queryPageByDataPerm(bizData4Page, conditions.get("sort").toString(), SqlOrderEnum.DESC);
        }
        return bizData4Page;
    }

    /**
	 * 新增信息
	 * @param ${objectName?uncap_first} 信息
     * @return 新增结果
     */
	@RequestMapping("/saveInfo")
	public boolean saveInfo(${objectName} ${objectName?uncap_first}) {
        ${objectName?uncap_first}Service.saveInfo(${objectName?uncap_first});
        return true;
	}

    /**
	 * 修改信息
	 * @param ${objectName?uncap_first} 信息
     * @return 修改结果
     */
	@RequestMapping("/updateInfo")
	public boolean updateInfo(${objectName} ${objectName?uncap_first}) {
        ${objectName?uncap_first}Service.updateInfo(${objectName?uncap_first});
        return true;
	}

    /**
	 * 删除信息
	 * @param ids 主键
     * @return 删除条数
     */
	@RequestMapping("/deleteInfoByIds")
	public int deleteInfoByIds(String ids) {
        return ${objectName?uncap_first}Service.deleteInfoByIds(ids);
	}

    /**
	 * 根据ID获取信息
	 * @param id 主键
	 * @return 信息
     */
	@RequestMapping("/getInfoById")
	public ${objectName} getInfoById(Long id) {
        ${objectName} ${objectName?uncap_first} = (${objectName}) ${objectName?uncap_first}Service.find(id);
		AssertUtil.notNull(${objectName?uncap_first}, "信息不存在");
		return ${objectName?uncap_first};
	}

}
